package com.zzptc.zzsgz.bdsjws.activities;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.widget.Toast;

import com.zzptc.zzsgz.bdsjws.MyApplication;
import com.zzptc.zzsgz.bdsjws.R;
import com.zzptc.zzsgz.bdsjws.activities.base.BaseActivity;
import com.zzptc.zzsgz.bdsjws.adapter.BBX_Adapter;
import com.zzptc.zzsgz.bdsjws.db.BBXDao;
import com.zzptc.zzsgz.bdsjws.views.DividerGridItemDecoration;
import com.zzptc.zzsgz.bdsjws.vo.BBX;

import java.util.Collections;
import java.util.List;

public class BBX_activity extends BaseActivity {

    private List<BBX> list;
    private RecyclerView recycler_view;
    private BBX_Adapter bbx_adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int getContentXML() {
        return R.layout.activity_bbx_activity;
    }

    @Override
    public void initData() {
        BBXDao dao= MyApplication.getDaoSession().getBBXDao();
        list=dao.loadAll();
    }

    @Override
    public void initView() {
//设定布局管理器
        recycler_view= (RecyclerView) findViewById(R.id.my_recycler_view);
        recycler_view.setLayoutManager(new GridLayoutManager(this,4));



//设置适配器
        bbx_adapter = new BBX_Adapter(list);
        recycler_view.setAdapter(bbx_adapter);
        recycler_view.addItemDecoration(new DividerGridItemDecoration(this, LinearLayoutManager.VERTICAL));
        helper.attachToRecyclerView(recycler_view);

    }

    ItemTouchHelper helper=new ItemTouchHelper(new ItemTouchHelper.Callback() {
    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        //首先回调的方法 返回int表示是否监听该方向
        int dragFlags = ItemTouchHelper.UP|ItemTouchHelper.DOWN|ItemTouchHelper.LEFT|ItemTouchHelper.RIGHT;//拖拽
        int swipeFlags = ItemTouchHelper.LEFT|ItemTouchHelper.RIGHT;//侧滑删除
        return makeMovementFlags(dragFlags,swipeFlags);

    }
    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        //滑动事件
        Toast.makeText(BBX_activity.this, "aaaa", Toast.LENGTH_SHORT).show();
        Collections.swap(list,viewHolder.getAdapterPosition(),target.getAdapterPosition());
        bbx_adapter.notifyItemMoved(viewHolder.getAdapterPosition(),target.getAdapterPosition());
        return false;
    }
    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        list.remove(viewHolder.getAdapterPosition());
        bbx_adapter.notifyItemRemoved(viewHolder.getAdapterPosition());
    }
    @Override
    public boolean isLongPressDragEnabled() {
        return true;
    }

    });
}
